import {Component} from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';

import request from 'superagent';

import {Backend} from '../../providers/backend';

@Component({
  templateUrl: 'build/pages/tab-resources/tab-resources-details.html'
})
export class Details {

  static get parameters() {
    return [[NavController], [NavParams], [Backend]];
  }

  constructor(navCtrl, navParams, backend) {
    this.id = navParams.get('id');
    this.classid = navParams.get('classid');
    this.item = navParams.get('item');
    this.navCtrl = navCtrl;
    this.navParams = navParams;
    this.backend = backend;
    this.inTheaters = {};
    this.menus = [];
    if(this.id){
      this.backend.contents(this.id,this.classid)
      .then(result => {
        this.menus = result;
      });
    }else{
      this.menus = this.item;
      this.menus['newspath']=this.item.newstime.substring(0, 10);
      console.log(this.menus);
    }
    
  }

  doRefresh(refresher) {
    this.backend.contents(this.id)
      .then(result => {
        this.menus = result;
        console.log(this.menus);
      });
    setTimeout(() => {
      console.log('Async operation has ended');
      refresher.complete();
    }, 2000);
  }

  _loadById(id) {
    return new Promise((resolve) => {
      request
        .get('http://10.3.1.2/v2/movie/subject/' + id)
        .end((err, res) => {
          resolve(res.body);
        });
    });
  }

  download($event) {

    console.log($event);

    if($event.target.nodeName == 'IMG' && $event.target.parentElement.nodeName == 'A') {
      console.log($event.target.parentElement.href);
      this.backend.fileDownload($event.target.parentElement.href);
    }

    $event.preventDefault();
    $event.stopPropagation();

  }
}
